package patrol.mobile.com.mobilepatrol.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.bean.GridViewAddTitleBean;
import patrol.mobile.com.mobilepatrol.bean.MenuBean;
import patrol.mobile.com.mobilepatrol.view.OnGridViewAddTitleLisener;

/**
 * Created by z on 2018/9/7.
 * Des: 自定列表空间
 */

public class GridViewAddTitleLayout extends LinearLayout {
    private View mView;
    private ViewHolder viewHolder;
    private ContentAdapter contentAdapter;
    private List<GridViewAddTitleBean> gridViewAddTitleBeans;
    private OnGridViewAddTitleLisener onGridViewAddTitleLisener;
    private OnTitleLayoutClickListener onTitleLayoutClickListener;
    List<MenuBean> menuBeanList;
    /**
     * 设置列表数据
     */
    public void setGridViewAddTitleBeans(List<GridViewAddTitleBean> gridViewAddTitleBeans, List<MenuBean> menuBeanList) {
        this.gridViewAddTitleBeans = gridViewAddTitleBeans;
        this.menuBeanList=menuBeanList;
        contentAdapter.notifyDataSetChanged();
    }

    /**
     * 设置title
     */
    public void setTitle(String title) {
        viewHolder.titleText.setText(title);
    }

    /**
     * 获取title右侧Imasge
     */
    public ImageView getTitleReightImage() {
        return viewHolder.titleImage;
    }

    /**
     * 设置title色块颜色
     */
    public void setViewColor(int mColor) {
        viewHolder.titleView.setBackgroundColor(mColor);
    }

    public void setTitleGone() {
        viewHolder.titleLayout.setVisibility(View.GONE);
    }

    /**
     * 设置item点击事件
     */
    public void setOnItemClickListener(OnGridViewAddTitleLisener onGridViewAddTitleLisener) {
        this.onGridViewAddTitleLisener = onGridViewAddTitleLisener;
    }

    public GridViewAddTitleLayout(Context context) {
        super(context);
        initView();
    }

    public GridViewAddTitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GridViewAddTitleLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.gridview_add_title, null);
        viewHolder = new ViewHolder(mView);
        viewHolder.contentRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        viewHolder.contentRecyclerView.setAdapter(contentAdapter = new ContentAdapter());
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mView.setLayoutParams(layoutParams);
        addView(mView);
        viewHolder.titleLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onTitleLayoutClickListener.onClick();
            }
        });
    }

    public View getTiTleView() {
        return viewHolder.titleLayout;
    }

    public void setOnTitleLayoutClickListener(OnTitleLayoutClickListener onTitleLayoutClickListener) {
        this.onTitleLayoutClickListener = onTitleLayoutClickListener;
    }

    public interface OnTitleLayoutClickListener {
        void onClick();
    }

    public class ViewHolder {
        @BindView(R.id.titleView)
        View titleView;
        @BindView(R.id.titleText)
        TextView titleText;
        @BindView(R.id.titleImage)
        ImageView titleImage;
        @BindView(R.id.titleLayout)
        RelativeLayout titleLayout;
        @BindView(R.id.contentRecyclerView)
        RecyclerView contentRecyclerView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {

        @Override
        public ContentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_content_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ContentAdapter.ViewHolder holder, final int position) {
            holder.itemTet.setText(gridViewAddTitleBeans.get(position).getTitle());
            holder.itemImage.setImageResource(gridViewAddTitleBeans.get(position).getImage());
            holder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onGridViewAddTitleLisener.onLisener(GridViewAddTitleLayout.this, position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return gridViewAddTitleBeans == null ? 0 : gridViewAddTitleBeans.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.itemImage)
            ImageView itemImage;
            @BindView(R.id.itemTet)
            TextView itemTet;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
